import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 用户个人主页界面
 * 跳转自LoginServlet的登录界面
 */

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前用户对应的会话对象，生成个性化信息
        HttpSession session = req.getSession(false);//登录了一定有会话，没登陆不新建会话
        if(session == null) {
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前尚未登录");
            return;
        }
        String username = (String) session.getAttribute("username");
        Long loginTime = (Long) session.getAttribute("loginTime");
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write("欢迎你 " + username + "! 上次登陆时间：" + loginTime);
    }
}
